<?php
// $Id: hotel_hall.php 399 2012-05-27 12:12:58Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Hotel_Hall_Action extends Action_Admin{
    var $shop_id;

    public function init(){
        parent::init();

        $this->shop_id = intval($this->params);
        $this->setVar('shop_id', $this->shop_id);

        $this->hall = App::module('table_hotel_hall', 'table', array($this->user));
        if(!$this->hall->check_user_right($this->shop_id)){
            $this->renderDialog('您无权管理此酒店信息！');
        }
    }

    public function onModify(){
        $this->setVar('show_validator', 1);

        list($shop_id, $hall_id) = explode('-', $this->params);
        $data = $this->hall->get_data($shop_id, $hall_id);
        if($data){
            $this->setVar('albums', $this->getAlbums());
            $this->setVar('data', $data);
            $this->render('admin/hotel/hall_edit');
        }else{
            $this->renderDialog('宴会厅不存在，无法读取信息！');
        }
    }

    public function onCreate(){
        $this->setVar('albums', $this->getAlbums());
        $this->setVar('show_validator', 1);
        $this->render('admin/hotel/hall_edit');
    }

    public function onList(){
        $list = $this->hall->get_list($this->shop_id);
        $this->setVar('list', $list);
        $this->render('admin/hotel/hall_list');
    }

    public function onSave(){
        if(array_get_bool($_POST, 'dosubmit')){
            switch(array_get_string($_POST, 'method')){
                case 'modify':
                    $hall_id = array_get_int($_POST, 'hall_id');
                    $this->hall->update($this->shop_id, $hall_id, $_POST);
                    $this->renderDialog('修改成功！', '', 'edit');
                break;
                case 'create':
                    $_POST['shop_id'] = $this->shop_id;
                    $this->hall->insert($_POST);
                    $this->renderDialog('新建成功！', '', 'add');
                break;
            }
        }else{
            $this->renderDialog('无效的参数！');
        }
    }

    public function onDelete(){
        $list = $this->hall->delete($this->shop_id, array_get_int($_GET, 'hall_id'));
        $this->renderDialog('删除成功！', '/admin/hotel_hall/list/'.$this->shop_id);
    }

    public function onSortlist(){
        if(array_get_bool($_POST, 'dosubmit')){
            $list = $this->hall->sort($this->shop_id, array_get($_POST, 'sorts'));
        }
        $list = $this->onList();
    }

    protected function getAlbums(){
        $this->album = App::module('table_album', 'table', array($this->user));

        $album_list = $this->album->get_list($this->shop_id, -1);
        $album_cats = $this->album->get_list_cat($this->shop_id);
        $albums = array();
        foreach($album_list as $item){
            $name = empty($item['name'])?'图片'.$item['album_id']:$item['name'];
            if(isset($album_cats[$item['album_cat']])){
                $cat = $album_cats[$item['album_cat']];
                $albums[$cat['album_cat'].'.'.$cat['cat_name']][$item['picture']] = $name;
            }else{
                $albums['0.默认相册'][$item['picture']] = $name;
            }
        }
        return $albums;
    }

    function __destruct(){}
}
?>